package com.ruoyi.web.controller.wnjk.registration;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.wnjk.domain.po.*;
import com.ruoyi.system.wnjk.domain.vo.RegistrationOrderVO;
import com.ruoyi.system.wnjk.domain.vo.doctorVO;
import com.ruoyi.system.wnjk.domain.vo.patientVO;
import com.ruoyi.system.wnjk.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 挂号Controller
 *
 * @author ruoyi
 * @date 2025-03-013
 */
@RestController
@RequestMapping("/app/registration")
public class RegistrationController extends BaseController {

    @Autowired
    private IWnjkHospitalService wnjkHospitalService;

    /**
     * 查询医院信息列表
     */
    @GetMapping("/listHospital")
    public TableDataInfo list(WnjkHospital wnjkHospital) {
        startPage();
        List<WnjkHospital> list = wnjkHospitalService.selectWnjkHospitalList(wnjkHospital);
        return getDataTable(list);
    }

    @Autowired
    private RegistrationOrderService registrationOrderService;

    /**
     * 查询挂号订单信息
     *
     * @return
     */
    @GetMapping("/listRegistrationOrders")
    public ResponseEntity<List<RegistrationOrderVO>> getAllRegistrationOrders() {
        List<RegistrationOrderVO> voList = registrationOrderService.getAllRegistrationOrders();
        return ResponseEntity.ok(voList);
    }

    @Autowired
    private IWnjkDepartmentService wnjkDepartmentService;

    /**
     * 查询科室信息（包含一级科室和二级科室）列表
     */
    @GetMapping("/listDepartment")
    public TableDataInfo list(WnjkDepartment wnjkDepartment) {
        List<WnjkDepartment> list = wnjkDepartmentService.selectWnjkDepartmentList(wnjkDepartment);
        return getDataTable(list);
    }

    /**
     * 根据医院ID查询医生列表
     */
    @GetMapping("/listDoctorsByHospitalId/{id}")
    public ResponseEntity<List<doctorVO>> getDoctorsByHospitalId(@PathVariable("id") Long Id) {
        List<doctorVO> doctors = registrationOrderService.getDoctorsByHospitalId(Id);
        return ResponseEntity.ok(doctors);
    }

    /**
     * 根据医生ID查询医生详细信息
     */
    @GetMapping("/listDoctorsById/{doctorId}")
    public ResponseEntity<doctorVO> getDoctorById(@PathVariable Long doctorId) {
        if (doctorId == null) {
            return ResponseEntity.badRequest().body(null);
        }

        doctorVO doctor = registrationOrderService.getDoctorById(doctorId);
        if (doctor == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }

        return ResponseEntity.ok(doctor);
    }

    @Autowired
    private IWnjkPatientService wnjkPatientService;

    /**
     * 查询就诊人信息列表
     */
    @GetMapping("/listPatients")
    public TableDataInfo list(WnjkPatient wnjkPatient) {
        List<WnjkPatient> list = wnjkPatientService.selectWnjkPatientList(wnjkPatient);
        return getDataTable(list);
    }

    /**
     * 根据用户ID查询相关联的患者信息
     */
    @GetMapping("/listByUserId/{userId}")
    public ResponseEntity<List<patientVO>> getPatientsByUserId(@PathVariable Long userId) {
        if (userId == null) {
            return ResponseEntity.badRequest().body(null);
        }

        List<patientVO> patients = registrationOrderService.getPatientsByUserId(userId);
        if (patients.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }

        return ResponseEntity.ok(patients);
    }

    /**
     * 根据患者ID删除患者信息
     */
    @DeleteMapping("/delete/{patientId}")
    public ResponseEntity<String> deletePatient(@PathVariable Long patientId) {
        if (patientId == null) {
            return ResponseEntity.badRequest().body("患者ID不能为空");
        }

        boolean success = registrationOrderService.deletePatient(patientId);
        if (success) {
            return ResponseEntity.ok("删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到对应患者");
        }
    }

    /**
     * 添加患者
     */
    @PostMapping("/add")
    public ResponseEntity<String> addPatient(@RequestBody Map<String, Object> map) {

        System.out.println("map = " + map);

        Object userIdObj = map.get("userId");
        Long userId = null;
        if (userIdObj instanceof Number) {
            userId = ((Number) userIdObj).longValue();
        } else if (userIdObj instanceof String) {
            userId = Long.valueOf((String) userIdObj);
        } else {
            throw new IllegalArgumentException("userId 格式不正确");
        }
        WnjkPatient p = new WnjkPatient();

        p.setIdCard((String) map.get("idCard"));
        p.setName((String) map.get("name"));
        p.setGender((String) map.get("gender"));


        boolean success = registrationOrderService.addPatient(userId, p);

        if (success) {
            return ResponseEntity.ok("添加成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("添加失败");
        }
    }

}
